package com.jstyle.jclife.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.jstyle.jclife.BuildConfig;
import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.model.AppUpdateData;
import com.jstyle.jclife.view.FileUpdateDialog;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2019/1/16.
 */

public class AppUpdateUtils {
    private static final String TAG = "AppUpdateUtils";
    public static void queryAppVersion(final Context context){
        Map<String, String> queryVersionMap= new HashMap<>();
        final String appVersion=getAppVersion(context);
        queryVersionMap.put(NetWorkConast.KEY_DEVICE_VERSION, appVersion);
        //queryVersionMap.put(NetWorkConast.KEY_DEVICE_VERSION, "1.1.7");
        queryVersionMap.put(NetWorkConast.KEY_AppName, "JC Life");
        NetWorkUtil.getInstance().getJstyleApi().queryAppVersion(queryVersionMap).
               compose(SchedulersTransformer.<AppUpdateData>io2MainObservable()).subscribe(new Observer<AppUpdateData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(AppUpdateData value) {
                int code = value.getMsgCode();
                Log.i(TAG, "onNext: "+code);
                if (code == NetWorkConast.SUCCESS) {
                    Log.i(TAG, "onNext: "+value.getData().getVersion());
                    String version="";
                    if(value.getData()!=null)version=value.getData().getVersion();
                    showUpdateDialog(context,version);
                }else{
                  //  checkUpdateFile();
                }

            }

            @Override
            public void onError(Throwable e) {

                Log.i(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private static void showUpdateDialog(final Context context,String appVersion) {
        FileUpdateDialog fileUpdateDialog = new FileUpdateDialog(context, R.style.jstyleDialog_style, appVersion);
        fileUpdateDialog.setFileUpdateListener(new FileUpdateDialog.FileUpdateListener() {
            @Override
            public void startUpdateFile() {
                toMarket(context, BuildConfig.APPLICATION_ID,"");
            }

            @Override
            public void laterUpdateFile() {

            }
        });
        fileUpdateDialog.setCancelable(false);
        fileUpdateDialog.show();

    }


    private static boolean toMarket(Context context, String appPkg, String marketPkg) {
        Uri uri = Uri.parse("market://details?id=" + appPkg);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        if (!TextUtils.isEmpty(marketPkg)) {// 如果没给市场的包名，则系统会弹出市场的列表让你进行选择。
            intent.setPackage(marketPkg);
        }
        try {
            context.startActivity(intent);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public static String getAppVersion(Context context) {
        PackageInfo packageInfo;
        String versionName = "";
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;

    }

}
